package com.basiclib.network

import android.content.Context

class RxErrorHandler(val builder: Builder) {

    val TAG = this.javaClass.simpleName
    private lateinit var mHandlerFactory: ErrorHandlerFactory

    init {
        this.mHandlerFactory = builder.errorHandlerFactory
    }

    companion object {
        fun builder(): Builder {
            return Builder()
        }
    }


    fun getHandlerFactory(): ErrorHandlerFactory {
        return mHandlerFactory
    }

    class Builder {
        private var context: Context? = null
        private var mResponseErrorListener: ResponseErrorListener? = null
        lateinit var errorHandlerFactory: ErrorHandlerFactory

        fun with(context: Context): Builder {
            this.context = context
            return this
        }

        fun responseErrorListener(responseErrorListener: ResponseErrorListener): Builder {
            this.mResponseErrorListener = responseErrorListener
            return this
        }

        fun build(): RxErrorHandler {
            if (context == null)
                throw IllegalStateException("Context is required")

            if (mResponseErrorListener == null)
                throw IllegalStateException("ResponseErrorListener is required")

            this.errorHandlerFactory = ErrorHandlerFactory(context, mResponseErrorListener)

            return RxErrorHandler(this)
        }
    }
}